// @ts-nocheck
import cornerstoneTools from "cornerstone-tools"
import { count } from "./colors"

const { eraseIfSegmentIndex } = cornerstoneTools.import(
  "util/segmentationUtils",
)
// CAUTION: below cannot erase and brush at the same time
export const drawBrushPixels = (
  pointArray,
  pixelData,
  segmentIndex, // always fixed to 1
  colorLUTIndex,
  columns,
  shouldErase = false,
  colorCountOn = false,
  imageIdIndex
) => {
  const getSpIndex = (x, y) => y * columns + x
  pointArray.forEach((point) => {
    const spIndex = getSpIndex(point.x, point.y)
    let index = -1
    if (colorCountOn) {
      !Array.isArray(pixelData.abc) && (pixelData.abc = []);
      index = pixelData.abc.indexOf(spIndex)
    }
    if (shouldErase) {
      if (colorCountOn && pixelData[spIndex] === segmentIndex) {
        count.decrease(colorLUTIndex)
      }
      eraseIfSegmentIndex(spIndex, pixelData, segmentIndex)
      if (colorCountOn) {
        index !== -1 && pixelData.abc.splice(index, 1)
      }
    } else {
      if (colorCountOn && pixelData[spIndex] !== segmentIndex) {
        count.increase(colorLUTIndex)
      }
      if (colorCountOn) {
        index === -1 && pixelData.abc.push(spIndex)
      }
      pixelData[spIndex] = segmentIndex
    }
  })
}
